﻿using System;
using System.IO.Packaging;
using System.Text;
using System.Windows.Controls;
using System.Xml;
using System.Collections.Generic;
using System.Windows.Documents;
using System.Windows;

namespace UIPrototyper
{
    public class CustomComboBox : ComboBox, ISelectable, IXML, IChanged, ICopy, IDocument 
    {
        Property property;
        private string description;

        public CustomComboBox()
        {
            property = new ComboBoxProperties(this);

            ((ComboBoxProperties)property).iChanged += new ChangedHandler(property_iChanged);
        }

        void property_iChanged(IChanged changedObject)
        {
            LaunchChangedEvent(changedObject);
        }

        #region ISelectable Members

        public Property Properties
        {
            get { return this.property; }
        }

        public string Description
        {
            get { return description; }
            set { description = value; }
        }

        #endregion

        #region IXML Members

        public string GenerateXML(Package p)
        {
            StringBuilder xml = new StringBuilder();

            xml.Append("<ComboBox description=\"");
            xml.Append(this.Description);
            xml.Append("\" ");

            xml.Append("margin=\"");
            xml.Append(this.Margin.ToString());
            xml.Append("\" ");

            xml.Append("height=\"");
            xml.Append(this.Height);
            xml.Append("\" ");

            xml.Append("width=\"");
            xml.Append(this.Width);
            xml.Append("\">");

            foreach (ItemProperties cbi in this.Items)
            {
                xml.Append("<Item content=\"" + cbi.Conteudo.ToString() + "\" />");
            }

            xml.Append("</ComboBox>");

            return xml.ToString();
        }

        public void LoadXML(XmlNode pXML)
        {
            this.Description = pXML.Attributes["description"].Value;
            this.Margin = MarginToThicknessConverter.ConvertToThickness(pXML.Attributes["margin"].Value);
            this.Height = Convert.ToDouble(pXML.Attributes["height"].Value.Replace('.', ','));
            this.Width = Convert.ToDouble(pXML.Attributes["width"].Value.Replace('.', ','));

            foreach (XmlNode node in pXML.ChildNodes)
            {
                ItemProperties cbi = new ItemProperties();

                cbi.Conteudo = node.Attributes["content"].Value;
                this.Items.Add(cbi);
            }
        }

        #endregion

        #region IChanged Members

        public event ChangedHandler iChanged;

        public void LaunchChangedEvent(IChanged pChangedObject)
        {
            if (iChanged != null)
            {
                iChanged(pChangedObject);
            }
        }

        #endregion

        #region ICopy Members

        public ISelectable Copy()
        {
            CustomComboBox myCopy = new CustomComboBox();

            myCopy.Description = this.description;

            myCopy.Margin = new System.Windows.Thickness(this.Margin.Left, this.Margin.Top, this.Margin.Right, this.Margin.Bottom);
            myCopy.Height = this.Height;
            myCopy.Width = this.Width;

            List<string> items = new List<string>();

            foreach (Object lc in this.Items)
            {
                items.Add(((ItemProperties)lc).Conteudo);
            }

            myCopy.ItemsSource = items;
            
            return myCopy;
        }

        #endregion

        #region IDocument Members

        public void Document(ref System.Windows.Documents.FlowDocument pDocument)
        {
            Paragraph controlDescription = new Paragraph();

            Run nameTitle = new Run("Nome: ");
            nameTitle.FontWeight = FontWeights.Bold;
            nameTitle.FontSize = 11;
            controlDescription.Inlines.Add(nameTitle);

            Run nameText = new Run(this.Name);
            nameText.FontSize = 11;
            controlDescription.Inlines.Add(nameText);
            controlDescription.Inlines.Add(new LineBreak());

            Run descriptionTitle = new Run("Descrição: ");
            descriptionTitle.FontWeight = FontWeights.Bold;
            descriptionTitle.FontSize = 11;
            controlDescription.Inlines.Add(descriptionTitle);

            Run descriptionText = new Run(this.Description);
            descriptionText.FontSize = 11;
            controlDescription.Inlines.Add(descriptionText);
            controlDescription.Inlines.Add(new LineBreak());

            pDocument.Blocks.Add(controlDescription);
        }

        #endregion
    }
}
